var calls = 0;

$(document).ready(function(){
	$(this).ajaxError(function(){
		showLoading(false);
		alert("There was an error completing your request, try again.");
	});
	
	$(this).ajaxStart(function(){
		showLoading(true);
	});
	
	$(this).ajaxComplete(function(){
		showLoading(false);
	});
	
	$(".fg-button").find("a").click(function(){
		showLoading(true);
	});
	
});

function showLoading(flag){
	if (calls > 0){
		return;
	}
		
	if (flag){
		$("#loadingPic").html('<img src="images/loading2.gif" style="margin: 12%"/>');
		$("#loadingPic").css("display: none; position:absolute; left: 0px; top:0px; height:100%; width: 100%; text-align: center; background-image:url('images/translucent.gif'); z-index: 1000;");
		$("#loadingPic").show();
	} else 
		$("#loadingPic").hide();
}

function formatCurrency(n) {  
	if (!isFinite(n)) {
        return n;
    }
    var s = ""+n, abs = Math.abs(n), _, i;
   if (abs >= 1000) {
        _  = (""+abs).split(/\./);
        i  = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
               _[0].slice(i).replace(/(\d{3})/g,',$1');

        s = _.join('.');
    }
   return s;
}

function shortenString(inputString, size){
	if(inputString.length > size+1) {
		inputString = inputString.substring(0,size)+"...";
	}
	return inputString;
}

function getColorCodeForVariableExpenses(percentage){
	var ten = "#7BC618";
	var twenty = "#94D639";
	var thirty = "#ADDE63";
	var forty = "#C6EF83";
	var fifty = "#D6EF39";
	var sixty = "#FFFF10";
	var seventy = "#FFFF6B";
	var eighty = "#FFB573";
	var ninety = "#FF6342";
	var more = "#FF0000";
	
	if (percentage == -1){
		return "#FFFFFF";
	} if (percentage < 10){
		return ten;
	} else if (percentage < 20){
		return twenty;
	}  else if (percentage < 30){
		return thirty;
	} else if (percentage < 40){
		return forty;
	} else if (percentage < 50){
		return fifty;
	} else if (percentage < 60){
		return sixty;
	} else if (percentage < 70){
		return seventy;
	} else if (percentage < 80){
		return eighty;
	} else if (percentage < 90){
		return ninety;
	} else if (percentage >= 90){
		return more;
	}
}

function getColorCodeForFixedExpenses(percentage){
	var ten = "#7BC618";
	var more = "#FF0000";
	
	if (percentage == -1){
		return "#FFFFFF";
	} else if (percentage == 0){
	 return "#F0F0F0";	
	}else	if (percentage < 101){
		return ten;
	} else if (percentage > 100){
		return more;
	}
}

function undoCurrencyFormatting(amount){
	return eval(amount.replace("R", "").replace(",", "").trim());
}

function correctAmountSign(amount) {
	amount = eval(amount);
	if (amount < 0) {
		amount = amount * -1;
	}
	return amount;
}

function padDateSingleDigit(digit){
	if (digit<10){
	 return "0"+ (digit +1);	
	} else {
		return (digit +1);
	}
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function validateInput(){
	$('input[numeric]').keyup(function(){
        var d=$(this).attr('numeric');
        var val=$(this).val();
        var orignalValue=val;
        val=val.replace(/[0-9]*/g, "");
       	
       	var msg="Only Integer Values allowed."; 
       	
       	if (d=='decimal'){
       	  value=value.replace(/\./, "");
          msg="Only Numeric Values allowed.";
          }
        
        if (val!=''){
        orignalValue=orignalValue.replace(/([^0-9].*)/g, "");
        $(this).val(orignalValue);
        alert(msg);
        }
      
    });
	
	 $('input[numeric]').change(function(){
	      // value is present
	    var tval=trim($(this).val());
	       if (tval=='') return true;
	         reg=/^0*/;
	         tval=tval.replace(reg,'');
	    
	         if (tval!='') 
	           val=parseInt(tval);
	         else
	           val=0;
	    var min=parseInt($(this).attr('min'));
	    var max=parseInt($(this).attr('max'));
	    var msg="";
	    
	    if(min!='' && max !=''){
	      msg='Input value should be in range of '+min + ' to ' + max + '.' ;
	    }
	    else{
	      if(min!=''){msg='Input value should be greter than or equal to'+min +'.';}
	      
	      else{
	        if(max!=''){msg='Input value should be less than or equal to '+ max +'.';}
	      }
	    }
	    if(min!=''){
	      if (min>val) {
	        alert(msg);
	        $(this).val('');
	        $(this).css('background',_alertColor);
	      }
	    }
	    
	    if (max!=''){
	      if (val>max) {
	        alert(msg);
	        $(this).val('');
	        $(this).css('background',_alertColor);
	      }
	    }
	  });
	// Accept valid email ID only

	  $('input[email]').keyup(function(){
	    $(this).css('background',_noAlertColor);
	  });
	       
	  $('input[email]').change(function(){
	    // value is numeric
	    str=($(this).val());        
	    var err=false;

	    if (trim(str)=='') return true;
	    var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i; 
	      
	    if ( filter.test(str) ) {
	      err=false; }
	    else  {
	      err=true; }
	     
	    if(err){
	      alert(str + ' is not a valid email address.');
	      $(this).val('');
	      $(this).css('background',_alertColor);
	    }
	  });          
}

Date.prototype.formatDate = function(format)

{

    var date = this;

    if (!format)

      format="MM/dd/yyyy";               

 

    var month = date.getMonth() + 1;

    var year = date.getFullYear();    

 

    format = format.replace("MM",month.toString().padL(2,"0"));        

 

    if (format.indexOf("yyyy") > -1)

        format = format.replace("yyyy",year.toString());

    else if (format.indexOf("yy") > -1)

        format = format.replace("yy",year.toString().substr(2,2));

 

    format = format.replace("dd",date.getDate().toString().padL(2,"0"));

 

    var hours = date.getHours();       

    if (format.indexOf("t") > -1)

    {

       if (hours > 11)

        format = format.replace("t","pm")

       else

        format = format.replace("t","am")

    }

    if (format.indexOf("HH") > -1)

        format = format.replace("HH",hours.toString().padL(2,"0"));

    if (format.indexOf("hh") > -1) {

        if (hours > 12) hours - 12;

        if (hours == 0) hours = 12;

        format = format.replace("hh",hours.toString().padL(2,"0"));        

    }

    if (format.indexOf("mm") > -1)

       format = format.replace("mm",date.getMinutes().toString().padL(2,"0"));

    if (format.indexOf("ss") > -1)

       format = format.replace("ss",date.getSeconds().toString().padL(2,"0"));

    return format;

}


String.repeat = function(chr,count)

{    

    var str = ""; 

    for(var x=0;x<count;x++) {str += chr}; 

    return str;

}

String.prototype.padL = function(width,pad)

{

    if (!width ||width<1)

        return this;   

 

    if (!pad) pad=" ";        

    var length = width - this.length

    if (length < 1) return this.substr(0,width);

 

    return (String.repeat(pad,length) + this).substr(0,width);    

}    

String.prototype.padR = function(width,pad)

{

    if (!width || width<1)

        return this;        

 

    if (!pad) pad=" ";

    var length = width - this.length

    if (length < 1) this.substr(0,width);

 

    return (this + String.repeat(pad,length)).substr(0,width);

} 